package com.aaron;

import org.apache.commons.lang.StringUtils;
import com.google.common.collect.Sets;

import java.util.Set;

/**
 * @author liurong
 * @date 2021/12/21 14:39
 * @version:
 */
public class ReplaceAllTest {

    private String mysql2dm(String sql) {
        Set<String> tableNames = Sets.newHashSet("R_AUTH_DATA_DEPT",
                "R_DEPT_REDUNDANCY",
                "R_ROLE_AUTHORITY",
                "R_USER_ROLE",
                "SREF_CON_TAB134221602_LEVEL",
                "SREF_CON_TAB134221602_REFED",
                "SREF_CON_TAB134221602_REFING",
                "S_AUTHORITY",
                "S_AUTHORIZE",
                "S_CONFIG_ITEM_CUSTOM",
                "S_CONFIG_ITEM_DEFAULT",
                "S_CONFIG_ITEM_MENU",
                "S_DATA_AUTHORITY",
                "S_DEPT",
                "S_MENU",
                "S_OPER_LOG",
                "S_OPER_LOGHISTORY",
                "S_PARAMETER_INFO",
                "S_PARAMETER_INFO_BUSINESS",
                "S_ROLE",
                "S_USER",
                "TR_EXT_ATTR_TYPE_VALUE",
                "TR_EXT_ATTR_VALUE",
                "TR_EXT_PARAM",
                "T_ABILITY",
                "T_CODE",
                "T_CONSTRUCT_MAPPING",
                "T_EXT_ATTR",
                "T_FAULT_DEVICE",
                "T_FIELD_STRATEGY",
                "T_FORWARD",
                "T_LESSEE_INFO",
                "T_MONITOR",
                "T_NON_VIDEO_BASE_INFO",
                "T_PARAM_CONSTRUCT",
                "T_PATH",
                "T_PLATFORM_INFO",
                "T_SEC_STRATEGY",
                "T_SUPPLIER",
                "T_VIDEO_BASE_INFO",
                "T_WARN",
                "T_YH_PARAMETER",
                "MMP_EQUIPMENT_TYPE",
                "MMP_EQUIPMENT_INFO",
                "MMP_COMPANY_INFO",
                "IMP_DEVICE_INFO"
        );

        for (String tableName : tableNames) {
            final String databaseAndTable = "ZLDAP." + tableName;

            // 已经做过达梦SQL改造
            if (StringUtils.containsIgnoreCase(sql, databaseAndTable)) {
                continue;
            }

            if (StringUtils.containsIgnoreCase(sql, tableName)) {
                // 正则匹配 空格/制表符/逗号 -> 忽略大小写 -> 表名 -> 空格/字符结束/制表符/换行符
//                sql = sql.replace(",", " , ");
                sql = sql.replaceAll(String.format("( |\t|)(?i)%s( |\n|\t|$)", tableName), String.format(" %s ", databaseAndTable));

            }
        }


        // 删除 ` 符号
        return sql.replace("`", "");

    }

    public static void main(String[] args) {

        String aaa = "            SELECT t2.aut_name FROM (\n" +
                "        SELECT u.login_name,u.password,u.p_salt,u1.role_id FROM s_user u LEFT JOIN\n" +
                "        (SELECT DISTINCT ur.* FROM r_user_role ur,s_role r WHERE\n" +
                "        r.role_id=ur.role_id AND r.role_id=?)\n" +
                "        u1\n" +
                "        ON u.user_id=u1.user_id WHERE u.login_name=?\n" +
                "        ) t1 LEFT JOIN (SELECT a.aut_name,a.aut_id,a.aut_url_method,ra.role_id FROM\n" +
                "        r_role_authority ra,\n" +
                "s_authority a WHERE a.state=1 AND ra.auth_id =\n" +
                "        a.aut_id) t2\n" +
                "        ON t1.role_id = t2.role_id";

        String sql = new ReplaceAllTest().mysql2dm(aaa);
        System.out.println(sql);

    }

}

